﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Reflection;
using StockEOD.Interfaces;
using SQLManager;

namespace StockEOD.Objects
{
    public class ManageDB :INotifyCaller
    {
        public  event StatusMessageDelgate OnStatusMessage;
        public  event PercentDoneDelegate OnPercentDoneChanged;
        public void fireStatusMessage(string message)
        {
            if (OnStatusMessage != null && !string.IsNullOrEmpty(message))
            {
                OnStatusMessage(message);

            }
        }
        public void firePercentChanged(float percent)
        {
            if (OnPercentDoneChanged != null)
            {
                OnPercentDoneChanged(percent);
            }
        }
        public  AppDataDataContext GetDbContext(string name, DirectoryInfo dir)
        {

            DirectoryInfo userFolder = new DirectoryInfo(Environment.GetEnvironmentVariable("SystemDrive") + @"\" + name);
            if (!Directory.Exists(userFolder.FullName))
            {
                fireStatusMessage(string.Format("Creating Directory {0}", userFolder));
                Directory.CreateDirectory(userFolder.FullName);
            }
            FileInfo stockDataFile = new FileInfo(System.IO.Path.Combine(userFolder.FullName, "StockData.mdf"));
            string conn = string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=true;Connect Timeout=30; User Instance=True; Integrated Security=SSPI;", stockDataFile.FullName);
            AppDataDataContext context = new AppDataDataContext(conn);
            if (!context.DatabaseExists())
            {
                try
                {
                    fireStatusMessage(string.Format("Creating a new database named"));
                    context.CreateDatabase();
                    // Create tables and relationships
                    CreateTables(context);
                    return context;
                }
                catch (SqlException err)
                {
                    fireStatusMessage(err.Message);
                    firePercentChanged(100);
                }

            }

            return null;

        }
        public static void CreateTables(AppDataDataContext context)
        {
            if(Directory.Exists(Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location)))
            {
                DirectoryInfo dApp = new DirectoryInfo(Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location));
                string sqlDBPath =string.Format(@"{0}\{1}",dApp.FullName,@"Objects\CreateDB.sql"); 
                if(File.Exists(sqlDBPath))
                {
                    string sqlCommand = File.ReadAllText(sqlDBPath);
                    SqlCommand command = new SqlCommand(sqlCommand);
                    context.ExecuteCommand(sqlCommand,null);
                }
            }
        }
    }
		
}
